package org.crackjack.android.activity;

import java.util.List;

import org.crackjack.android.R;
import org.crackjack.android.adapter.AppointmentBinder;
import org.crackjack.android.adapter.GenericAdapter;
import org.crackjack.android.model.Appointment;
import org.crackjack.android.util.AppointmentUtil;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;

public class AppointmentListActivity extends GenericActivity implements OnClickListener, OnItemClickListener {

    private ListView appointmentList;
    private List<Appointment> appointments;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        appointments = getIntent().getParcelableArrayListExtra("appointments");
        appointmentList = (ListView) findViewById(R.id.appointment_list);
        appointmentList.setAdapter(new GenericAdapter<Appointment>(this, new AppointmentBinder(), appointments));
        appointmentList.setOnItemClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
        }

    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        Long appointmentId = (Long) arg1.getTag();
        Appointment appointment = AppointmentUtil.getAppoitmentById(appointmentId, appointments);
        Intent intent = new Intent(this, AppointmentActivity.class);
        if (appointment != null) {
            Bundle bundle = new Bundle();
            bundle.putParcelable("appointment", appointment);
            intent.putExtras(bundle);
        } else {
            Toast.makeText(this, "There is no data about appointment", 500).show();
        }

        startActivity(intent);

    }

}
